package controllers.weixin;

import auth.WxMpAuth;
import models.contract.BankCard;
import models.contract.Contract;
import models.contract.ContractCardRelation;
import models.contract.ContractRoom;
import models.enums.AuthorizationStatus;
import models.enums.BankCardType;
import models.user.User;
import org.apache.commons.collections.map.HashedMap;
import org.json.JSONObject;
import play.mvc.Controller;
import play.mvc.With;

import java.util.*;

@With(WxMpAuth.class)
public class BankCardController extends Controller {


    public static void index() {
        User user = WxMpAuth.currentUser();
        List<ContractCardRelation> contractCardRelations = ContractCardRelation.findAllByUser(user);
        if(contractCardRelations == null || contractCardRelations.size() == 0) {
            String msg = "请先绑定门禁卡!";
            flash.error(msg);
            redirect("/user/index");
        }
        Map<String,Object> map = new HashMap<String,Object>();
        List<ContractCardRelation> contractCardRelationList = new ArrayList<ContractCardRelation>();
        for (ContractCardRelation contractCardRelation :contractCardRelations) {
            if(map.get(contractCardRelation.contract.id) == null) {
                map.put(contractCardRelation.contract.id,contractCardRelation);
                contractCardRelationList.add(contractCardRelation);
            }
        }
        List<BankCard> bankCards = new ArrayList<BankCard>();
        for (ContractCardRelation contractCardRelation :contractCardRelationList) {
            BankCard bankCard = BankCard.findByContract(contractCardRelation.contract);
            if(bankCard != null) {
                bankCard.bankCardNumber = bankCard.bankCardNumber.substring(bankCard.bankCardNumber.length()-4,bankCard.bankCardNumber.length());
                bankCards.add(bankCard);
            }
        }
        render(bankCards);
    }

    public static void addNewBankCard() {
        User user = WxMpAuth.currentUser();
        List<ContractCardRelation> contractCardRelations = ContractCardRelation.findAllByUser(user);
        if(contractCardRelations == null || contractCardRelations.size() == 0) {
            String msg = "请先绑定门禁卡";
            flash.error(msg);
            redirect("/user/index");
        }
        Map<String,Object> map = new HashMap<String,Object>();
        List<Contract> contractList = new ArrayList<Contract>();
        for (ContractCardRelation contractCardRelation :contractCardRelations) {
            if(map.get(contractCardRelation.contract.id) == null) {
                map.put(contractCardRelation.contract.id,contractCardRelation);
                contractList.add(contractCardRelation.contract);
            }
        }
        render(contractList);
    }

    public static void bankCardInfo(String bankCardId) {
        System.out.println("bankCardId="+bankCardId);
        BankCard bankCard = BankCard.findById(bankCardId);
        if(bankCard != null) {
            bankCard.bankCardNumber = bankCard.bankCardNumber.substring(bankCard.bankCardNumber.length()-4,bankCard.bankCardNumber.length());
        }
        render(bankCard);
    }

    /**
     * 绑定银行卡
     * @param bankCardNumber
     */
    public static void bind(String bankCardNumber,String contractCode) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", false);
        //TODO 验证 bankCardNumber
        User user = WxMpAuth.currentUser();
        // 虚拟验证银行卡信息
        JSONObject bankJson = bankCallBackJson(bankCardNumber);
        //如果验证通过
        if(true) {
            ContractCardRelation contractCardRelation = ContractCardRelation.findByUserAndCode(user,contractCode);
            if(contractCardRelation == null) {
                resultMap.put("msg", "请先绑定门禁卡!");
                renderJSON(resultMap);
            }
            BankCard bankCard1 = BankCard.findByContract(contractCardRelation.contract);
            if(bankCard1 != null) {
                resultMap.put("msg", "您的银行卡已绑定合同");
                renderJSON(resultMap);
            }
            BankCard bankCard = new BankCard();
            bankCard.bankCardNumber = bankJson.get("bankNumber").toString();
            bankCard.bankName = bankJson.get("bankName").toString();
            bankCard.bankCardType = BankCardType.PERSONAL; // 暂定个人银行卡
            bankCard.bankUUId = bankJson.get("uuid").toString();
            bankCard.contract = contractCardRelation.contract;
            bankCard.user = user;
            bankCard.withholdAuthorization = AuthorizationStatus.HAVE_NOT;
            bankCard.createdAt = new Date();
            bankCard.save();
            resultMap.put("success", true);
            resultMap.put("msg", "银行卡绑定成功");
            renderJSON(resultMap);
        }
        resultMap.put("msg", "您的银行卡信息验证失败");
        renderJSON(resultMap);
    }


    /**
     * 虚拟一个 银行卡信息 json
     * @param bankCardNumber 卡号
     * @return
     */
    public static JSONObject bankCallBackJson(String bankCardNumber) {
        Map<String, Object> resultMap = new HashedMap();
        resultMap.put("bankNumber" , bankCardNumber);
        resultMap.put("bankName" , "中国工商银行");
        resultMap.put("bankUserName" , "张三");
        resultMap.put("uuid" , "sflsdkfjlsdfjkslfj");
        return new JSONObject(resultMap);
    }
}
